package com.zbkj.front.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

public class MyLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {

        //方式一：得到请求头参数，解析Accept-Language
        String header = httpServletRequest.getHeader("Accept-Language");
        Locale locale = Locale.getDefault();
        if(header != null && header.length()>0){
            System.out.println(header);
            if (!StringUtils.isEmpty(header)) {
                String[] s = header.split("-");
                locale = new Locale(s[0],s[1]);
            }
        }


       /* //方式二：得到cookie,解析locale
        Cookie[] cookies = httpServletRequest.getCookies();
        Locale locale = Locale.getDefault();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName());
                System.out.println(cookie.getValue());
                if (cookie.getName().equals("lang")) {
                    String temp = cookie.getValue();
                    if (!StringUtils.isEmpty(temp)) {
                        String[] s = temp.split("-");
                        locale = new Locale(s[0], s[1]);
                    }
                }
            }
        }
*/
        /*//方式三：通过参数解析locale
        String temp = httpServletRequest.getParameter("lang");
        Locale locale = Locale.getDefault();
        if (!StringUtils.isEmpty(temp)) {
            String[] s = temp.split("-");
            locale = new Locale(s[0],s[1]);
        }*/

        return locale;
    }


    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
